package com.hanhai.zrb.backend.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hanhai.common.annotation.PermissionNeed;
import com.hanhai.common.util.MD5Util;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.backend.admin.dao.mybatis.PrivilegeDao;
import com.hanhai.zrb.backend.admin.service.RoleService;
import com.hanhai.zrb.backend.admin.service.UserRoleService;

@Controller
@RequestMapping("/realuser")
public class RealUserController extends BaseController {

	@Resource
	private PrivilegeDao privilegeDao;

	@Resource
	private UserRoleService userRoleService;

	@Resource
	private RoleService roleService;

	@PermissionNeed("privilege_realuser_list")
	@RequestMapping("")
	public String index() {
		return "realuser/list";
	}

	@PermissionNeed("privilege_realuser_list")
	@RequestMapping("/list")
	@ResponseBody
	public String list() throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> list = userRoleService.listAll();
		int total = list.size();
		map.put("rows", list);
		map.put("total", total);
		return JSONObject.toJSONString(map);
	}

	@PermissionNeed("privilege_realuser_management")
	@RequestMapping(value = "/doadd", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String doadd(@RequestParam Map<String, Object> params) {
		md5(params);
		JSONObject jsonObject = new JSONObject();
		if (userRoleService.add(params)) {
			jsonObject.put("success", true);
			jsonObject.put("msg", "添加成功！");
		} else {
			jsonObject.put("success", false);
			jsonObject.put("msg", "添加失败！");
		}
		return jsonObject.toJSONString();
	}

	@RequestMapping("initRole")
	@ResponseBody
	public String initRole() {
		List<Map<String, Object>> roleList = roleService.listAll();
		return JSONObject.toJSONString(roleList);
	}

	@PermissionNeed("privilege_realuser_management")
	@RequestMapping(value = "/doupdate", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String doupdate(@RequestParam Map<String, Object> params) {
		md5(params);
		JSONObject jsonObject = new JSONObject();
		if (userRoleService.update(params)) {
			jsonObject.put("success", true);
			jsonObject.put("msg", "更新成功！");
		} else {
			jsonObject.put("success", false);
			jsonObject.put("msg", "更新失败！");
		}
		return jsonObject.toJSONString();
	}

	private void md5(Map<String, Object> params) {
		String key = "password";
		String password = MapUtils.getString(params, key);
		if (StringUtils.isNotBlank(password)) {
			String mdPassword = MD5Util.sha1(password);
			params.put(key, mdPassword);
		} else {
			params.remove(key);
		}
	}
}
